<?php
error_reporting(E_ALL ^ E_NOTICE);
//超时设置
if($public_r['php_outtime'])
{
	@set_time_limit($public_r['php_outtime']);
}

//页面编码
if($phome_headercharset==1)
{
	if($phome_ecms_charver=='gb2312'||$phome_ecms_charver=='big5'||$phome_ecms_charver=='utf-8')
	{
		@header('Content-Type: text/html; charset='.$phome_ecms_charver);
	}
}

//禁止IP
eCheckAccessIp(0);

//EMAIL地址检查
function  chemail($email){
	if(!ereg("^[_\.0-9A-z-]+@([0-9A-z][0-9A-z-]+\.)+[A-z]{2,3}$",$email))
	{
		return false;
	}
	else
	{
		return true;
	}
}

//上传文件
function DoTranFile($file,$file_name,$file_type,$file_size,$classid){
	global $public_r,$class_r,$addgethtmlpath,$doetran;
	$filetype=GetFiletype($file_name);//取得文件类型
	$r[insertfile]=md5(uniqid(microtime()));
	$r[filename]=$r[insertfile].$filetype;
	$r[filepath]=FormatFilePath($classid,$mynewspath,0);//按日期存放文件
	$filepath=$r[filepath]?$r[filepath].'/':$r[filepath];
	$r[tran]=1;
	//按栏目
	$fspath=ReturnFileSavePath($classid);
	$path1=$addgethtmlpath."../../".$fspath['filepath'].$filepath;
	$r[url]=$fspath['fileurl'].$filepath.$r[filename];
	$r[savepath]=$path1;
	$r[name]=$path1."small".$r[insertfile];
	$path=$path1.$r[filename];
	$r[yname]=$path;
	//文件类型
	if(CheckSaveTranFiletype($filetype))
	{
		if($doetran)
		{
			$r[tran]=0;
			return $r;
		}
		else
		{
			printerror("TranFail","history.go(-1)");
		}
	}
	//上传文件
	$cp=@move_uploaded_file($file,$path);
	if(empty($cp))
	{
		if($doetran)
		{
			$r[tran]=0;
			return $r;
		}
		else
		{
			printerror("TranFail","history.go(-1)");
		}
	}
	DoChmodFile($path);
	$r[filesize]=$file_size;
	return $r;
}

//删除附件
function DoDelFile($r){
	global $class_r,$addgethtmlpath;
	$path=$r['path']?$r['path'].'/':$r['path'];
	$fspath=ReturnFileSavePath($r[classid],$r[fpath]);
	$delfile=$addgethtmlpath."../../".$fspath['filepath'].$path.$r['filename'];
	$del=DelFiletext($delfile);
}

//自定义字段返回模板字段处理
function doReturnAddTempf($temp){
	$record="<!--record-->";
	$field="<!--field--->";
	$r=explode($record,$temp);
	$count=count($r);
	for($i=0;$i<$count-1;$i++)
	{
		$r1=explode($field,$r[$i]);
		$str.=",".$r1[1];
	}
	return $str;
}

//返回字段
function ReturnAddF($modid,$rdata=0){
	global $empire,$dbtbpre;
	$record="<!--record-->";
	$field="<!--field--->";
	$mr=$empire->fetch1("select tempvar,enter,listandf,setandf,listtempvar from {$dbtbpre}enewsmod where mid='$modid'");
	//模板字段
	if($rdata==1)//内容
	{
		$ret_r['tempvar']=doReturnAddTempf($mr['tempvar']);
	}
	elseif($rdata==2)//列表
	{
		$ret_r['listtempvar']=doReturnAddTempf($mr['listtempvar']);
	}
	else//全部
	{
		$ret_r['tempvar']=doReturnAddTempf($mr['tempvar']);
		$ret_r['listtempvar']=doReturnAddTempf($mr['listtempvar']);
	}
	$ret_r['listandf']=$mr['listandf'];
	$ret_r['setandf']=$mr['setandf'];
	return $ret_r;
}

//替换php代码
function RepPhpAspJspcode($string){
	//$string=str_replace("<?xml","[!--ecms.xml--]",$string);
	$string=str_replace("<?","&lt;?",$string);
	$string=str_replace("<%","&lt;%",$string);
	//$string=str_replace("[!--ecms.xml--]","<?xml",$string);
	return $string;
}

//增加收藏
function AddFava($id,$classid,$cid,$from){
	global $empire,$level_r,$class_r,$dbtbpre;
	//是否登陆
	$user_r=islogin();
	$id=(int)$id;
	$cid=(int)$cid;
	$classid=(int)$classid;
	if(empty($id)||empty($classid))
	{
		printerror("ErrorUrl","history.go(-1)",1);
    }
	//表不存在
	if(empty($class_r[$classid][tbname]))
	{
		printerror("ErrorUrl","history.go(-1)",1);
	}
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where id='$id' and classid='$classid'");
	if(empty($num))
	{printerror("ErrorUrl","history.go(-1)",1);}
	//是否已收藏
	$newsnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsfava where id='$id' and classid='$classid' and userid='$user_r[userid]'");
	if($newsnum)
	{
		printerror("ReFava","history.go(-1)",1);
	}
	$favanum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsfava where userid='$user_r[userid]'");
	$groupid=$user_r[groupid];
	if($level_r[$groupid][favanum]<=$favanum)
	{
		printerror("MoreFava","history.go(-1)",1);
	}
	$favatime=date("Y-m-d H:i:s");
	$sql=$empire->query("insert into {$dbtbpre}enewsfava(id,favatime,userid,username,classid,cid) values($id,'$favatime',$user_r[userid],'$user_r[username]',$classid,$cid);");
	if($sql)
	{
		printerror("AddFavaSuccess",$from,1);
	}
	else
	{
		printerror("DbError","history.go(-1)",1);
	}
}

//批量删除收藏
function DelFava_All($favaid){
	global $empire,$dbtbpre;
	//是否登陆
	$user_r=islogin();
	$count=count($favaid);
	if(empty($count))
	{printerror("NotDelFavaid","history.go(-1)",1);}
	for($i=0;$i<$count;$i++)
	{
		$add.="favaid='".intval($favaid[$i])."' or ";
    }
	$add=substr($add,0,strlen($add)-4);
	$sql=$empire->query("delete from {$dbtbpre}enewsfava where (".$add.") and userid='$user_r[userid]'");
	if($sql)
	{printerror("DelFavaSuccess","../member/fava",1);}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//删除单个收藏夹
function DelFava($favaid){
	global $empire,$dbtbpre;
	//是否登陆
	$user_r=islogin();
	$favaid=(int)$favaid;
	if(empty($favaid))
	{printerror("NotDelFavaid","history.go(-1)",1);}
	$sql=$empire->query("delete from {$dbtbpre}enewsfava where favaid='$favaid' and userid='$user_r[userid]'");
	if($sql)
	{printerror("DelFavaSuccess","../member/fava",1);}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//点卡冲值
function CardGetFen($username,$reusername,$card_no,$password){
	global $empire,$user_tablename,$user_userfen,$user_userid,$user_username,$user_userdate,$dbtbpre,$user_zgroup,$user_group;
	$card_no=RepPostVar($card_no);
	$password=RepPostVar($password);
	$username=RepPostVar($username);
	if(!trim($username)||!trim($card_no)||!trim($password))
	{
		printerror("EmptyGetCard","history.go(-1)",1);
	}
	if($username!=$reusername)
	{
		printerror("DifCardUsername","history.go(-1)",1);
	}
	//编码转换
	$utfusername=doUtfAndGbk($username,0);
	$user=$empire->fetch1("select ".$user_userid.",".$user_userdate." from ".$user_tablename." where ".$user_username."='".$utfusername."' limit 1");
	if(!$user[$user_userid])
	{printerror("ExiestCardUsername","history.go(-1)",1);}
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewscard where card_no='".$card_no."' and password='".$password."' limit 1");
	if(!$num)
	{printerror("CardPassError","history.go(-1)",1);}
	//是否过期
	$buytime=date("Y-m-d H:i:s");
	$r=$empire->fetch1("select cardfen,money,endtime,carddate,cdgroupid,cdzgroupid from {$dbtbpre}enewscard where card_no='$card_no' limit 1");
	if($r[endtime]<>"0000-00-00")
	{
		$endtime=to_date($r[endtime]);
		if($endtime<time())
		{
			printerror("CardOutDate","history.go(-1)",1);
	    }
    }
	//包月
	if($r[carddate])
	{
		if($user[$user_userdate]<time())
		{
			$userdate=time()+$r[carddate]*24*3600;
		}
		else
		{
			$userdate=$user[$user_userdate]+$r[carddate]*24*3600;
		}
		$a="";
		//转向会员组
		if($r['cdgroupid'])
		{
			$a=",".$user_group."=".$r[cdgroupid];
		}
		if($r['cdzgroupid'])
		{
			$a.=",".$user_zgroup."=".$r[cdzgroupid];
		}
		$sql=$empire->query("update ".$user_tablename." set ".$user_userdate."='".$userdate."'".$a." where ".$user_username."='$utfusername'");
	}
	else
	{
		$sql=$empire->query("update ".$user_tablename." set ".$user_userfen."=".$user_userfen."+$r[cardfen] where ".$user_username."='$utfusername'");
	}
	if($sql)
	{
		$sql1=$empire->query("delete from {$dbtbpre}enewscard where card_no='$card_no'");//删除卡号
		$sql2=$empire->query("insert into {$dbtbpre}enewsbuybak(userid,username,card_no,cardfen,money,buytime,userdate) values('".$user[$user_userid]."','$username','$card_no',$r[cardfen],$r[money],'$buytime',$r[carddate]);");//备份购买记录
		printerror("CardGetFenSuccess","../member/card",1);
	}
	else
	{printerror("CardGetFenError","history.go(-1)",1);}
}

//--------下载软件
function DownSoft($classid,$id,$pathid,$p,$pass){
	global $empire,$public_r,$level_r,$class_r,$user_tablename,$user_userid,$user_rnd,$user_group,$user_userfen,$user_userdate,$user_username,$user_todaydate,$user_todaydown,$dbtbpre;
	$id=(int)$id;
	$classid=(int)$classid;
	$pathid=(int)$pathid;
	if(empty($id)||empty($p)||empty($classid))
	{printerror("ErrorUrl","history.go(-1)",1);}
	$p=RepPostVar($p);
	$p_r=explode(":::",$p);
	$userid=$p_r[0];
	$rnd=$p_r[1];
	//验证码
	$cpass=md5("wm_chief".$public_r[downpass].$userid);
	if($cpass<>$pass)
	{
		printerror("FailDownpass","history.go(-1)",1);
    }
	//表不存在
	if(empty($class_r[$classid][tbname]))
	{
		printerror("ExiestSoftid","history.go(-1)",1);
	}
	$ok=1;
	$r=$empire->fetch1("select downpath,id,title from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where id='$id' and classid='$classid'");
	if(empty($r[id]))
	{printerror("ExiestSoftid","history.go(-1)",1);}
	//区分下载地址
	$path_r=explode("\r\n",$r[downpath]);
	$showdown_r=explode("::::::",$path_r[$pathid]);
	$downgroup=$showdown_r[2];
	//下载权限
	if($downgroup)
	{
		$userid=(int)$userid;
		//取得会员资料
		$u=$empire->fetch1("select * from ".$user_tablename." where ".$user_userid."='$userid' and ".$user_rnd."='$rnd'");
		if(empty($u[$user_userid]))
		{printerror("MustSingleUser","history.go(-1)",1);}
		//下载次数限制
		$setuserday="";
		if($level_r[$u[$user_group]][daydown])
		{
			$thetoday=date("Y-m-d");
			if($thetoday!=$u[$user_todaydate])
			{
				$setuserday="update ".$user_tablename." set ".$user_todaydate."='".$thetoday."',".$user_todaydown."=1 where ".$user_userid."='$userid'";
			}
			else
			{
				if($u[$user_todaydown]>=$level_r[$u[$user_group]][daydown])
				{
					printerror("CrossDaydown","history.go(-1)",1);
				}
				$setuserday="update ".$user_tablename." set ".$user_todaydown."=".$user_todaydown."+1 where ".$user_userid."='$userid'";
			}
		}
		if($level_r[$downgroup][level]>$level_r[$u[$user_group]][level])
		{
			printerror("NotDownLevel","history.go(-1)",1);
		}
		//点数是否足够
		$showdown_r[3]=intval($showdown_r[3]);
		if($showdown_r[3])
		{
			//---------是否有历史记录
			$bakr=$empire->fetch1("select id,truetime from {$dbtbpre}enewsdownrecord where id='$id' and classid='$classid' and userid='$userid' and pathid='$pathid' and online=0 order by truetime desc limit 1");
			if($bakr[id]&&(time()-$bakr[truetime]<=$public_r[redodown]*3600))
			{}
			else
			{
				//包月卡
				if($u[$user_userdate]-time()>0)
				{}
				//点数
				else
				{
					if($showdown_r[3]>$u[$user_userfen])
					{
						printerror("NotEnoughFen","history.go(-1)",1);
					}
					//去除点数
					$usql=$empire->query("update ".$user_tablename." set ".$user_userfen."=".$user_userfen."-".$showdown_r[3]." where ".$user_userid."='$userid'");
				}
				//备份下载记录
				$utfusername=doUtfAndGbk($u[$user_username],1);
				BakDown($classid,$id,$pathid,$userid,$utfusername,$r[title],$showdown_r[3],0);
			}
		}
		//更新用户下载次数
		if($setuserday)
		{
			$usql=$empire->query($setuserday);
		}
	}
	//总下载数据增一
    $usql=$empire->query("update {$dbtbpre}ecms_".$class_r[$classid][tbname]." set totaldown=totaldown+1 where id='$id'");
    $downurl=stripSlashes($showdown_r[1]);
	$downurlr=ReturnDownQzPath($downurl,$showdown_r[4]);
	$downurl=$downurlr['repath'];
	//防盗链
	@include("../class/enpath.php");
	$downurl=DoEnDownpath($downurl);
    db_close();
    $empire=null;
	DoTypeForDownurl($downurl,$downurlr['downtype']);
}

//下载操作
function DoTypeForDownurl($downurl,$type=0){
	global $public_r;
	if($type==1)//meta
	{
		echo"<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;url=$downurl\">";
	}
	elseif($type==2)//read
	{
		QDownLoadFile($downurl);
	}
	else//header
	{
		Header("Location:$downurl");
	}
	exit();
}

//下载
function QDownLoadFile($file){
	global $public_r;
	if(strstr($file,"\\"))
	{
		$exp="\\";
	}
	elseif(strstr($file,"/"))
	{
		$exp="/";
	}
	else
	{
		Header("Location:$file");
		exit();
	}
	if(strstr($file,$exp."e".$exp)||strstr($file,"..")||strstr($file,"?")||strstr($file,"#"))
	{
		Header("Location:$file");
		exit();
    }
	if(strstr($file,$public_r[fileurl]))
	{
		$file=str_replace($public_r[fileurl],'/d/file/',$file);
	}
	if(!strstr($file,"://"))
	{
		if(!file_exists($file))
		{
			$file="../..".$file;
		}
	}
	$filename=GetDownurlFilename($file,$exp);
	if(empty($filename))
	{
		Header("Location:$file");
		exit();
	}
	//下载
	Header("Content-type: application/octet-stream");
	//Header("Accept-Ranges: bytes");
	//Header("Accept-Length: ".$filesize);
	Header("Content-Disposition: attachment; filename=".$filename);
	echo ReadFiletext($file);
}

//取得下载文件名
function GetDownurlFilename($file,$expstr){
	$r=explode($expstr,$file);
	$count=count($r)-1;
	$filename=$r[$count];
	return $filename;
}

//----------------------在线电影模型
//取得验证码
function GetOnlinePass(){
	global $public_r;
	$onlinep=$public_r[downpass]."qweirtydui4opttt.,mvcfvxzzf3dsfm,.dsa";
	$r[0]=time();
	$r[1]=md5($onlinep.$r[0]);
	return $r;
}

//验证验证码
function CheckOnlinePass($onlinetime,$onlinepass){
	global $movtime,$public_r;
	if($onlinetime+$movtime<time()||$onlinetime>time())
	{
		exit();
	}
	$onlinep=$public_r[downpass]."qweirtydui4opttt.,mvcfvxzzf3dsfm,.dsa";
	$cpass=md5($onlinep.$onlinetime);
	if($onlinepass<>$cpass)
	{
		exit();
	}
}

//--------取得软件地址
function GetSofturl($classid,$id,$pathid,$p,$pass,$onlinetime,$onlinepass){
	global $empire,$public_r,$class_r,$level_r,$user_tablename,$user_userid,$user_username,$user_rnd,$user_group,$user_userfen,$user_userdate,$user_todaydate,$user_todaydown,$dbtbpre,$realplayertype,$mediaplayertype;
	$classid=(int)$classid;
	$id=(int)$id;
	$pathid=(int)$pathid;
	$onlinetime=(int)$onlinetime;
	$p=RepPostVar($p);
	if(!$classid||empty($id)||empty($p))
	{exit();}
	$p_r=explode(":::",$p);
	$userid=$p_r[0];
	$rnd=$p_r[1];
	//验证码
	$cpass=md5("wm_chief".$public_r[downpass].$userid);
	if($cpass<>$pass)
	{exit();}
	//验证验证码
	CheckOnlinePass($onlinetime,$onlinepass);
	//表不存在
	if(empty($class_r[$classid][tbname]))
	{exit();}
	$r=$empire->fetch1("select id,onlinepath,title from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where id='$id' and classid='$classid'");
	if(empty($r[id]))
	{exit();}
	//区分下载地址
	$path_r=explode("\r\n",$r[onlinepath]);
	$showdown_r=explode("::::::",$path_r[$pathid]);
	$downgroup=$showdown_r[2];
	//下载权限
	if($downgroup)
	{
		$userid=(int)$userid;
		//取得会员资料
		$u=$empire->fetch1("select * from ".$user_tablename." where ".$user_userid."='$userid' and ".$user_rnd."='$rnd'");
		if(empty($u[$user_userid]))
		{exit();}
		//下载次数限制
		$setuserday="";
		if($level_r[$u[$user_group]][daydown])
		{
			$thetoday=date("Y-m-d");
			if($thetoday!=$u[$user_todaydate])
			{
				$setuserday="update ".$user_tablename." set ".$user_todaydate."='".$thetoday."',".$user_todaydown."=1 where ".$user_userid."='$userid'";
			}
			else
			{
				if($u[$user_todaydown]>=$level_r[$u[$user_group]][daydown])
				{
					exit();
				}
				$setuserday="update ".$user_tablename." set ".$user_todaydown."=".$user_todaydown."+1 where ".$user_userid."='$userid'";
			}
		}
		if($level_r[$downgroup][level]>$level_r[$u[$user_group]][level])
		{
			exit();
		}
		//点数是否足够
		$showdown_r[3]=intval($showdown_r[3]);
		if($showdown_r[3])
		{
			//---------是否有历史记录
		    $bakr=$empire->fetch1("select id,truetime from {$dbtbpre}enewsdownrecord where id='$id' and classid='$classid' and userid='$userid' and pathid='$pathid' and online=1 order by truetime desc limit 1");
			if($bakr[id]&&(time()-$bakr[truetime]<=$public_r[redodown]*3600))
			{}
			else
			{
				//包月卡
				if($u[$user_userdate]-time()>0)
				{}
				//点数
				else
				{
			       if($showdown_r[3]>$u[$user_userfen])
			       {
					   exit();
			       }
			       //去除点数
				   $usql=$empire->query("update ".$user_tablename." set ".$user_userfen."=".$user_userfen."-".$showdown_r[3]." where ".$user_userid."='$userid'");
				}
				//备份下载记录
				$utfusername=doUtfAndGbk($u[$user_username],1);
				BakDown($classid,$id,$pathid,$userid,$utfusername,$r[title],$showdown_r[3],1);
			}
		}
		//更新用户下载次数
		if($setuserday)
		{
			$usql=$empire->query($setuserday);
		}
	}
	//总下载数据增一
    $usql=$empire->query("update {$dbtbpre}ecms_".$class_r[$classid][tbname]." set totaldown=totaldown+1 where id='$id'");
	//选择播放器
	$ftype=GetFiletype($showdown_r[1]);
	if(strstr($realplayertype,','.$ftype.','))
	{
		Header("Content-Type: audio/x-pn-realaudio");
	}
	else
	{
		Header("Content-Type: video/x-ms-asf");
	}
    $downurl=stripSlashes($showdown_r[1]);
	$downurlr=ReturnDownQzPath($downurl,$showdown_r[4]);
	$downurl=$downurlr['repath'];
	//防盗链
	@include("../class/enpath.php");
	$downurl=DoEnOnlinepath($downurl);
    db_close();
    $empire=null;
	echo $downurl;
	exit();
}

//-----------------------------------商城模型开始
//声名购物车
function SetBuycar($buycar){
	$set=esetcookie("mybuycar",$buycar,0);
	return $set;
}

//清空购物车
function ClearBuycar(){
	SetBuycar("");
	Header("Refresh:0; URL=../ShopSys/buycar");
}

//加入购物车
function AddBuycar($classid,$id){
	global $class_r,$empire,$dbtbpre;
	$classid=(int)$classid;
	$id=(int)$id;
	if(empty($classid)||empty($id)||empty($class_r[$classid][tbname]))
	{
		printerror("NotChangeProduct","history.go(-1)",1);
    }
	//验证产品是否存在
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where classid='$classid' and id='$id' limit 1");
	if(!$num)
	{
		printerror("NotChangeProduct","history.go(-1)",1);
	}
	$record="!";
	$field="|";
	$productid=$classid.",".$id;
	$buycar=getcvar('mybuycar');
	//重复
	if(strstr($buycar,"|".$productid."|"))
	{
		$pr=explode("|".$productid."|",$buycar);
		$pr1=explode("!",$pr[1]);
		$oldbuycar="|".$productid."|".$pr1[0]."!";
		//数量
		if(empty($pr1[0]))
		{
			$pr1[0]=1;
		}
		$newnum=$pr1[0]+1;
		$newbuycar="|".$productid."|".$newnum."!";
		$buycar=str_replace($oldbuycar,$newbuycar,$buycar);
	}
	else
	{
		$buycar.="|".$productid."|1!";
	}
	SetBuycar($buycar);
	Header("Refresh:0; URL=../ShopSys/buycar");
}

//修改购物车
function EditBuycar($add){
	$record="!";
	$field="|";
	$productid=$add['productid'];
	$num=$add['num'];
	$del=$add['del'];
	$count=count($productid);
	$buycar="";
	for($i=0;$i<$count;$i++)
	{
		$productid[$i]=RepPostVar($productid[$i]);
		$num[$i]=intval($num[$i]);
		//验证是否删除项
		if(empty($num[$i]))
		{
			continue;
	    }
		$isdel=0;
		for($j=0;$j<count($del);$j++)
		{
			if($del[$j]==$productid[$i])
			{
				$isdel=1;
				break;
			}
		}
		if($isdel==1)
		{
			continue;
		}
		$buycar.="|".$productid[$i]."|".$num[$i]."!";
    }
	SetBuycar($buycar);
	Header("Refresh:0; URL=../ShopSys/buycar");
}

//增加订单
function AddDd($add){
	global $empire,$user_tablename,$user_money,$user_userid,$user_userfen,$user_rnd,$public_r,$dbtbpre;
	//购物车无内容
	if(!getcvar('mybuycar'))
	{
		printerror("EmptyBuycar","history.go(-1)",1);
    }
	$add[ddno]=RepPostVar($add[ddno]);
	$add[truename]=RepPostStr($add[truename]);
	$add[oicq]=RepPostStr($add[oicq]);
	$add[msn]=RepPostStr($add[msn]);
	$add[call]=RepPostStr($add[call]);
	$add[phone]=RepPostStr($add[phone]);
	$add[email]=RepPostStr($add[email]);
	$add[address]=RepPostStr($add[address]);
	$add[zip]=RepPostStr($add[zip]);
	$add[bz]=RepPostStr($add[bz]);
	$add[g_truename]=RepPostStr($add[g_truename]);
	$add[g_oicq]=RepPostStr($add[g_oicq]);
	$add[g_msn]=RepPostStr($add[g_msn]);
	$add[g_call]=RepPostStr($add[g_call]);
	$add[g_phone]=RepPostStr($add[g_phone]);
	$add[g_email]=RepPostStr($add[g_email]);
	$add[g_address]=RepPostStr($add[g_address]);
	$add[g_zip]=RepPostStr($add[g_zip]);
	$add[fptt]=RepPostStr($add[fptt]);
	$add[fp]=(int)$add[fp];
	$add[psid]=(int)$add[psid];
	$add[payfsid]=(int)$add[payfsid];
	if(!$add[truename]||!$add[call]||!$add[email]||!$add[address]||!$add[g_truename]||!$add[g_call]||!$add[g_address]||!$add[g_email]||!$add[psid]||!$add[payfsid])
	{
		printerror("MustEnterSelect","history.go(-1)",1);
    }
	$mess="AddDdSuccess";
	$haveprice=0;
	$payby=0;
	//返回购物车存放格式
	$buyr=ReturnBuycardd();
	$alltotal=$buyr[2];
	$alltotalfen=$buyr[1];
	$buycar=$buyr[3];
	//发票
	$fptotal=0;
	if($add[fp])
	{
		$fptotal=$alltotal*($public_r[fpnum]/100);
	}
	//配送方式
	$pr=$empire->fetch1("select pid,pname,price from {$dbtbpre}enewsshopps where pid='$add[psid]'");
	if(empty($pr[pid]))
	{
		printerror("NotPsid","history.go(-1)",1);
	}
	//支付方式
	$payr=$empire->fetch1("select payid,payname,payurl,userpay,userfen from {$dbtbpre}enewsshoppayfs where payid='$add[payfsid]'");
	if(empty($payr[payid]))
	{
		printerror("NotPayfsid","history.go(-1)",1);
	}
	//取得用户信息
	$userid=(int)getcvar('mluserid');
	$username=RepPostVar(getcvar('mlusername'));
	if($userid)
	{
		$rnd=RepPostVar(getcvar('mlrnd'));
		$user=$empire->fetch1("select ".$user_userid.",".$user_money.",".$user_userfen." from ".$user_tablename." where ".$user_userid."='$userid' and ".$user_rnd."='$rnd' limit 1");
		if(!$user[$user_userid])
		{
			printerror("MustSingleUser","history.go(-1)",1);
		}
	}
	$location="../ShopSys/buycar";
	//直接扣点
	if($payr[userfen])
	{
		if($buyr[0])
		{
			printerror("NotProductForBuyfen","history.go(-1)",1);
		}
		else
		{
			if($userid)
			{
				$buyallfen=$alltotalfen+$pr[price];
				if($buyallfen>$user[$user_userfen])
				{
					printerror("NotEnoughFenBuy","history.go(-1)",1);
				}
				//扣除点数
				$usql=$empire->query("update ".$user_tablename." set ".$user_userfen."=".$user_userfen."-".$buyallfen." where ".$user_userid."='$userid'");
				if($usql)
				{
					$mess="AddDdSuccessa";
					$payby=1;
					$haveprice=1;
				}
			}
			else
			{
				printerror("NotLoginTobuy","history.go(-1)",1);
			}
		}
	}
	//帐号余额扣除
	elseif($payr[userpay])
	{
		    if($userid)
			{
				$buyallmoney=$alltotal+$pr[price]+$fptotal;
				if($buyallmoney>$user[$user_money])
				{
					printerror("NotEnoughMoneyBuy","history.go(-1)",1);
				}
				//扣除金额
				$usql=$empire->query("update ".$user_tablename." set ".$user_money."=".$user_money."-".$buyallmoney." where ".$user_userid."='$userid'");
				if($usql)
				{
					$mess="AddDdSuccessa";
					$payby=2;
					$haveprice=1;
				}
			}
			else
			{
				printerror("NotLoginTobuy","history.go(-1)",1);
			}
	}
	//在线支付
	elseif($payr[payurl])
	{
		$mess="AddDdAndToPaySuccess";
		$location=$payr[payurl];
	}
	else
	{}
	$ddtime=date("Y-m-d H:i:s");
	$pr[price]=(float)$pr[price];
	$alltotal=(float)$alltotal;
	$alltotalfen=(float)$alltotalfen;
	$fptotal=(float)$fptotal;
	$sql=$empire->query("insert into {$dbtbpre}enewsshopdd(ddno,ddtime,userid,username,outproduct,haveprice,checked,truename,oicq,msn,email,`call`,phone,address,zip,bz,g_truename,g_oicq,g_msn,g_email,g_call,g_phone,g_address,g_zip,buycar,psid,psname,pstotal,alltotal,payfsid,payfsname,payby,alltotalfen,fp,fptt,fptotal) values('$add[ddno]','$ddtime',$userid,'$username',0,'$haveprice',0,'$add[truename]','$add[oicq]','$add[msn]','$add[email]','$add[call]','$add[phone]','$add[address]','$add[zip]','$add[bz]','$add[g_truename]','$add[g_oicq]','$add[g_msn]','$add[g_email]','$add[g_call]','$add[g_phone]','$add[g_address]','$add[g_zip]','".addslashes($buycar)."','$add[psid]','$pr[pname]',$pr[price],$alltotal,'$add[payfsid]','$payr[payname]','$payby',$alltotalfen,$add[fp],'$add[fptt]',$fptotal);");
	if($sql)
	{
		$ddid=$empire->lastid();
		$set=esetcookie("paymoneyddid",$ddid,0);
		SetBuycar("");
		printerror($mess,$location,1);
	}
	else
	{
		printerror("DbError","history.go(-1)",1);
	}
}

//返回购物车数据
function ReturnBuycardd(){
	global $empire,$class_r,$dbtbpre;
	$buycar=getcvar('mybuycar');
	$record="!";
	$field="|";
	$r=explode($record,$buycar);
	$alltotal=0;
	$return[0]=0;//是否全部积分
	$return[1]=0;//购买总积分
	$return[2]=0;//购买总金额
	$return[3]="";//存放格式
	$newbuycar="";
	for($i=0;$i<count($r)-1;$i++)
	{
		$pr=explode($field,$r[$i]);
		$productid=$pr[1];
		$fr=explode(",",$pr[1]);
		//ID
		$classid=(int)$fr[0];
		$id=(int)$fr[1];
		if(empty($class_r[$classid][tbname]))
		{
			continue;
		}
		//数量
		$num=(int)$pr[2];
		if(empty($num))
		{
			$num=1;
		}
		//取得产品信息
		$productr=$empire->fetch1("select title,tprice,price,titleurl,groupid,classid,newspath,filename,id,titlepic,buyfen from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where classid='$classid' and id='$id' limit 1");
		if(empty($productr[id]))
		{
			continue;
		}
		//是否全部积分
		if(!$productr[buyfen])
		{
			$return[0]=1;
		}
		$return[1]+=$productr[buyfen]*$num;
		$thistotal=$productr[price]*$num;
		$alltotal+=$thistotal;
		//组成存放的格式
		$title=str_replace("!","",$productr[title]);
		$title=str_replace("|","",$title);
		$title=str_replace(",","",$title);
		$newbuycar.="|".$classid.",".$id."|".$num."|".$productr[price]."|".$productr[buyfen]."|".$title."!";
    }
	$return[2]=$alltotal;
	$return[3]=$newbuycar;
	return $return;
}

//返回分类表
function ReturnFavaClassTb($ecms){
	global $dbtbpre;
	if(empty($ecms))//收藏
	{
		$r['tb']="enewsfavaclass";
		$r['url']="../member/fava/FavaClass";
	}
	else//好友
	{
		$r['tb']="enewshyclass";
		$r['url']="../member/friend/FriendClass";
	}
	$r['tb']=$dbtbpre.$r['tb'];
	return $r;
}

//---------------------增加收藏夹分类
function AddFavaClass($add){
	global $empire,$dbtbpre;
	if(!trim($add[cname]))
	{
		printerror("EmptyFavaClassname","history.go(-1)",1);
    }
	//是否登陆
	$user_r=islogin();
	$add[cname]=RepPostStr($add[cname]);
	//返回表
	$tbr=ReturnFavaClassTb($add['doing']);
	$sql=$empire->query("insert into ".$tbr['tb']."(cname,userid) values('$add[cname]','$user_r[userid]');");
	if($sql)
	{printerror("AddFavaClassSuccess",$tbr['url'],1);}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//---------------------修改收藏夹分类
function EditFavaClass($add){
	global $empire,$dbtbpre;
	$add[cid]=(int)$add[cid];
	if(!trim($add[cname])||!$add[cid])
	{
		printerror("EmptyFavaClassname","history.go(-1)",1);
    }
	//是否登陆
	$user_r=islogin();
	$add[cname]=RepPostStr($add[cname]);
	//返回表
	$tbr=ReturnFavaClassTb($add['doing']);
	$sql=$empire->query("update ".$tbr['tb']." set cname='$add[cname]' where cid='$add[cid]' and userid='$user_r[userid]'");
	if($sql)
	{printerror("EditFavaClassSuccess",$tbr['url'],1);}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//---------------------删除收藏夹分类
function DelFavaClass($cid,$doing=0){
	global $empire,$dbtbpre;
	$cid=(int)$cid;
	if(!$cid)
	{
		printerror("EmptyFavaClassid","history.go(-1)",1);
    }
	//是否登陆
	$user_r=islogin();
	//返回表
	$tbr=ReturnFavaClassTb($doing);
	$sql=$empire->query("delete from ".$tbr['tb']." where cid='$cid' and userid='$user_r[userid]'");
	//$sql1=$empire->query("delete from {$dbtbpre}enewsfava where cid='$cid' and userid='$user_r[userid]'");
	if($sql)
	{printerror("DelFavaClassSuccess",$tbr['url'],1);}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//---------------------返回收藏夹分类
function ReturnFavaclass($userid,$cid,$doing=0){
	global $empire,$dbtbpre;
	//返回表
	$tbr=ReturnFavaClassTb($doing);
	$sql=$empire->query("select cid,cname from ".$tbr['tb']." where userid='$userid' order by cid");
	$select="";
	while($r=$empire->fetch($sql))
	{
		if($r[cid]==$cid)
		{$selected=" selected";}
		else
		{$selected="";}
		$select.="<option value=".$r[cid].$selected.">".$r[cname]."</option>";
    }
	return $select;
}

//-----------------------批量转移收藏
function MoveFava_All($favaid,$cid){
	global $empire,$dbtbpre;
	//是否登陆
	$user_r=islogin();
	$cid=(int)$cid;
	if(!$cid)
	{printerror("NotChangeMoveCid","history.go(-1)",1);}
	$count=count($favaid);
	if(empty($count))
	{printerror("NotMoveFavaid","history.go(-1)",1);}
	for($i=0;$i<$count;$i++)
	{
		$add.="favaid='".intval($favaid[$i])."' or ";
    }
	$add=substr($add,0,strlen($add)-4);
	$sql=$empire->query("update {$dbtbpre}enewsfava set cid=$cid where (".$add.") and userid='$user_r[userid]'");
	if($sql)
	{printerror("MoveFavaSuccess","../member/fava",1);}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//处理入库变量值
function doaddslashes($data){
	if(!get_magic_quotes_gpc())
	{
		$data=addslashes($data);
	}
	return $data;
}

//提交反馈信息
function AddFeedback($add){
	global $empire,$dbtbpre,$level_r,$public_r;
	CheckCanPostUrl();//验证来源
	$bid=(int)getcvar('feedbackbid');
	if(empty($bid))
	{
		$bid=intval($add[bid]);
	}
	if(empty($bid))
	{
		printerror("EmptyFeedbackname","history.go(-1)",1);
    }
	//版面是否存在
	$br=$empire->fetch1("select bid,enter,mustenter,filef,groupid from {$dbtbpre}enewsfeedbackclass where bid='$bid';");
	if(empty($br['bid']))
	{
		printerror("EmptyFeedback","history.go(-1)",1);
	}
	//权限
	if($br['groupid'])
	{
		$user=islogin();
		if($level_r[$br[groupid]][level]>$level_r[$user[groupid]][level])
		{
			printerror("HaveNotEnLevel","history.go(-1)",1);
		}
	}
	$pr=$empire->fetch1("select feedbacktfile,feedbackfilesize,feedbackfiletype from {$dbtbpre}enewspublic limit 1");
	//必填项
	$mustr=explode(",",$br['mustenter']);
	$count=count($mustr);
	for($i=1;$i<$count-1;$i++)
	{
		$mf=$mustr[$i];
		if(strstr($br['filef'],",".$mf.","))//附件
		{
			if(!$pr['feedbacktfile'])
			{
				printerror("NotOpenFBFile","",1);
			}
			if(!$_FILES[$mf]['name'])
			{
				printerror("EmptyFeedbackname","",1);
			}
		}
		else
		{
			if(!trim($add[$mf]))
			{
				printerror("EmptyFeedbackname","",1);
			}
		}
	}
	$saytime=date("Y-m-d H:i:s");
	//字段处理
	$dh="";
	$tranf="";
	$record="<!--record-->";
	$field="<!--field--->";
	$er=explode($record,$br['enter']);
	$count=count($er);
	for($i=0;$i<$count-1;$i++)
	{
		$er1=explode($field,$er[$i]);
		$f=$er1[1];
		//附件
		$add[$f]=str_replace('[!#@-','',$add[$f]);
		if(strstr($br['filef'],",".$f.","))
		{
			if($_FILES[$f]['name'])
			{
				if(!$pr['feedbacktfile'])
				{
					printerror("NotOpenFBFile","",1);
				}
				$filetype=GetFiletype($_FILES[$f]['name']);//取得文件类型
				if(CheckSaveTranFiletype($filetype))
				{
					printerror("NotQTranFiletype","",1);
				}
				if(!strstr($pr['feedbackfiletype'],"|".$filetype."|"))
				{
					printerror("NotQTranFiletype","",1);
				}
				if($_FILES[$f]['size']>$pr['feedbackfilesize']*1024)//文件大小
				{
					printerror("TooBigQTranFile","",1);
				}
				$tranf.=$dh.$f;
				$dh=",";
				$fval="[!#@-".$f."-@!]";
			}
			else
			{
				$fval="";
			}
		}
		else
		{
			$fval=RepPostStr($add[$f]);
		}
		$addf.=",`".$f."`";
		$addval.=",'".doaddslashes($fval)."'";
	}
	$type=0;
	$userid=(int)getcvar('mluserid');
	$username=RepPostVar(getcvar('mlusername'));
	//上传附件
	if($tranf)
	{
		$filepath=date("Y-m-d");
		$path1="../../d/file/p/".$filepath;
		$mk=DoMkdir($path1);//不存在则建立目录
		$dh="";
		$tranr=explode(",",$tranf);
		$count=count($tranr);
		for($i=0;$i<$count;$i++)
		{
			$tf=$tranr[$i];
			//文件名
			$filetype=GetFiletype($_FILES[$tf]['name']);//取得文件类型
			$insertfile=md5(uniqid(microtime()));
			$tranfilename=$insertfile.$filetype;
			$path=$path1."/".$tranfilename;
			//上传文件
			$cp=@move_uploaded_file($_FILES[$tf]['tmp_name'],$path);
			if($cp)
			{
				DoChmodFile($path);
				//写入数据库
				$filetime=$saytime;
				$filesize=(int)$_FILES[$tf]['size'];
				$classid=(int)$classid;
				$sql=$empire->query("insert into {$dbtbpre}enewsfile(filename,filesize,adduser,path,filetime,classid,no,type,id,cjid,fpath) values('$tranfilename',$filesize,'[Member]".$username."','$filepath','$filetime',$classid,'[FB]".addslashes(RepPostStr($add[title]))."','$type',0,0,'$public_r[fpath]');");
				$repfval=$filepath."/".$tranfilename;
				$filename.=$dh.$tranfilename;
				$dh=",";
			}
			else
			{
				$repfval="";
			}
			$addval=str_replace("[!#@-".$tf."-@!]",$repfval,$addval);
		}
	}
	$ip=egetip();
	$sql=$empire->query("insert into {$dbtbpre}enewsfeedback(bid,saytime,ip,filepath,filename,userid,username".$addf.") values('$bid','$saytime','$ip','$filepath','$filename',$userid,'$username'".$addval.");");
	if($sql)
	{
		$reurl=DoingReturnUrl("../tool/feedback?bid=$bid",$add['ecmsfrom']);
		printerror("AddFeedbackSuccess",$reurl,1);
	}
	else
	{printerror("DbError","history.go(-1)",1);}
}

//--------------发送错误报告
function AddError($add){
	global $empire,$class_r,$dbtbpre;
	CheckCanPostUrl();//验证来源
	$id=(int)$add['id'];
	$classid=(int)$add['classid'];
	if(!$classid||!$id||!trim($add[errortext]))
	{printerror("EmptyErrortext","history.go(-1)",1);}
	//返回标题链接
	if(empty($class_r[$classid][tbname]))
	{
		printerror("ErrorUrl","history.go(-1)",1);
    }
	$r=$empire->fetch1("select titleurl,groupid,classid,newspath,filename,id from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where id='$id' and classid='$classid' limit 1");
	if(empty($r[id]))
	{
		printerror("ErrorUrl","history.go(-1)",1);
    }
	$titleurl=sys_ReturnBqTitleLink($r);
	$email=RepPostStr($add[email]);
	$ip=egetip();
	$errortext=RepPostStr($add[errortext]);
	$errortime=date("Y-m-d H:i:s");
	$sql=$empire->query("insert into {$dbtbpre}enewsdownerror(id,errortext,errorip,errortime,email,classid) values($id,'$errortext','$ip','$errortime','$email',$classid);");
	if($sql)
	{
		printerror("AddErrorSuccess",$titleurl,1);
	}
	else
	{
		printerror("DbError","history.go(-1)",1);
	}
}

//-------------------------取得随机数
function GetFileMd5(){
	$p=md5(uniqid(microtime()));
	return $p;
}

//-------------------------建立存放目录
function MkDirTxtFile($date,$file){
	global $do_txtpath;
	$r=explode("/",$date);
	$path=$do_txtpath.$r[0];
	DoMkdir($path);
	$path=$do_txtpath.$date;
	DoMkdir($path);
	$returnpath=$date."/".$file;
	return $returnpath;
}

//--------------修改文本字段内容
function EditTxtFieldText($pagetexturl,$pagetext){
	global $do_txtpath;
	$pagetext="<? exit();?>".$pagetext;
	$file=$do_txtpath.$pagetexturl.".php";
	WriteFiletext_n($file,$pagetext);
}

//--------------读取文本字段内容
function GetTxtFieldText($pagetexturl){
	global $do_txtpath;
	$file=$do_txtpath.$pagetexturl.".php";
	$text=ReadFiletext($file);
	$text=substr($text,12);//去除exit
	return $text;
}

//发送短信息
function AddMsg($add){
	global $empire,$level_r,$dbtbpre,$user_tablename,$user_userid,$user_username,$user_group,$user_havemsg;
	$user=islogin();
	$title=RepPostStr(trim($add['title']));
	$to_username=RepPostVar(trim($add['to_username']));
	$msgtext=RepPostStr($add['msgtext']);
	if(empty($title)||!trim($msgtext)||empty($to_username))
	{printerror("EmptyMsg","",1);}
	if($user['username']==$to_username)
	{printerror("MsgToself","",1);}
	//字数
	$len=strlen($msgtext);
	if($len>$level_r[$user[groupid]][msglen])
	{
		printerror("MoreMsglen","",1);
	}
	//接收方是否存在
	$utfto_username=doUtfAndGbk($to_username,0);//编码转换
	$r=$empire->fetch1("select ".$user_userid.",".$user_group." from ".$user_tablename." where ".$user_username."='$utfto_username' limit 1");
	if(!$r[$user_userid])
	{
		printerror("MsgNotToUsername","",1);
    }
	//对方短信息是否满
	$mnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsqmsg where to_username='$to_username'");
	if($mnum+1>$level_r[$r[$user_group]][msgnum])
	{
		printerror("UserMoreMsgnum","",1);
	}
	$msgtime=date("Y-m-d H:i:s");
	$sql=$empire->query("insert into {$dbtbpre}enewsqmsg(title,msgtext,haveread,msgtime,to_username,from_userid,from_username,outbox,issys) values('".addslashes($title)."','".addslashes($msgtext)."',0,'$msgtime','$to_username','$user[userid]','$user[username]',0,0);");
	//放入发件箱
	if($add['inout'])
	{
		//短信息是否满
		$mynum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsqmsg where to_username='$user[username]'");
		if($mynum+1<=$level_r[$user[groupid]][msgnum])
	    {
			$isql=$empire->query("insert into {$dbtbpre}enewsqmsg(title,msgtext,haveread,msgtime,to_username,from_userid,from_username,outbox,issys) values('".addslashes($title)."','".addslashes($msgtext)."',1,'$msgtime','$user[username]','$user[userid]','$user[username]',1,0);");
	    }
	}
	$usql=$empire->query("update {$user_tablename} set ".$user_havemsg."=1 where ".$user_username."='$utfto_username' limit 1");
	if($sql)
	{
		printerror("AddMsgSuccess","../member/msg?out=$add[inout]",1);
	}
	else
	{printerror("DbError","",1);}
}

//保存发件箱
function AddOutMsg($add){
	global $empire,$level_r,$dbtbpre;
	$user=islogin();
	$title=RepPostStr($add['title']);
	$msgtext=RepPostStr($add['msgtext']);
	if(empty($title)||!trim($msgtext))
	{printerror("EmptyMsg","",1);}
	//字数
	$len=strlen($msgtext);
	if($len>$level_r[$user[groupid]][msglen])
	{
		printerror("MoreMsglen","",1);
	}
	//短信息是否满
	$mynum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsqmsg where to_username='$user[username]'");
	if(($mynum+1>$level_r[$user[groupid]][msgnum]))
	{
		printerror("SelfMoreMsgnum","",1);
	}
	$msgtime=date("Y-m-d H:i:s");
	$sql=$empire->query("insert into {$dbtbpre}enewsqmsg(title,msgtext,haveread,msgtime,to_username,from_userid,from_username,outbox,issys) values('".addslashes($title)."','".addslashes($msgtext)."',1,'$msgtime','$user[username]','$user[userid]','$user[username]',1,0);");
	if($sql)
	{
		printerror("AddOutmsgSuccess","",1);
	}
	else
	{printerror("DbError","",1);}
}

//删除短信息
function DelMsg($mid,$out){
	global $empire,$dbtbpre;
	$user=islogin();
	$mid=(int)$mid;
	if(!$mid)
	{printerror("EmptyDelMsg","",1);}
	$sql=$empire->query("delete from {$dbtbpre}enewsqmsg where mid='$mid' and to_username='$user[username]'");
	if($sql)
	{
		printerror("DelMsgSuccess","../member/msg?out=$out",1);
    }
	else
	{printerror("DbError","",1);}
}

//批量删除短信息
function DelMsg_all($mid,$out){
	global $empire,$dbtbpre;
	$user=islogin();
	$count=count($mid);
	if(!$count)
	{printerror("EmptyDelMsg","",1);}
	for($i=0;$i<$count;$i++)
	{
		$add.="mid='".intval($mid[$i])."' or ";
    }
	$add=substr($add,0,strlen($add)-4);
	$sql=$empire->query("delete from {$dbtbpre}enewsqmsg where (".$add.") and to_username='$user[username]'");
    if($sql)
	{
		printerror("DelMsgSuccess","../member/msg?out=$out",1);
    }
    else
	{printerror("DbError","",1);}
}

//替换全局模板变量
function ReplaceTempvar($temp){
	global $empire;
	if(empty($temp))
	{return $temp;}
	$sql=$empire->query("select myvar,varvalue from ".GetTemptb("enewstempvar")." where isclose=0 order by myorder desc,varid");
	while($r=$empire->fetch($sql))
	{
		$myvar="[!--temp.".$r[myvar]."--]";
		$temp=str_replace($myvar,$r[varvalue],$temp);;
    }
	return $temp;
}

//增加好友
function AddFriend($add){
	global $empire,$dbtbpre,$user_tablename,$user_username;
	//是否登陆
	$user_r=islogin();
	$fname=RepPostVar(trim($add['fname']));
	if(!$fname)
	{
		printerror("EmptyFriend","",1);
	}
	//加自己为好友
	if($fname==$user_r['username'])
	{
		printerror("NotAddFriendSelf","",1);
	}
	$utfusername=doUtfAndGbk($fname,0);
	$num=$empire->gettotal("select count(*) as total from ".$user_tablename." where ".$user_username."='$utfusername' limit 1");
	if(!$num)
	{
		printerror("NotFriendUsername","",1);
	}
	//重复提交
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewshy where fname='$fname' and userid='$user_r[userid]' limit 1");
	if($num)
	{
		printerror("ReAddFriend","",1);
	}
	$cid=(int)$add['cid'];
	$fsay=RepPostStr($add['fsay']);
	$sql=$empire->query("insert into {$dbtbpre}enewshy(userid,fname,cid,fsay) values('$user_r[userid]','".addslashes($fname)."',$cid,'".addslashes($fsay)."');");
	if($sql)
	{
		printerror("AddFriendSuccess","../member/friend?cid=$add[fcid]",1);
	}
	else
	{
		printerror("DbError","",1);
	}
}

//修改好友
function EditFriend($add){
	global $empire,$dbtbpre,$user_tablename,$user_username;
	//是否登陆
	$user_r=islogin();
	$fid=(int)$add['fid'];
	$fname=RepPostVar(trim($add['fname']));
	if(!$fname||!$fid)
	{
		printerror("EmptyFriend","",1);
	}
	//加自己为好友
	if($fname==$user_r['username'])
	{
		printerror("NotAddFriendSelf","",1);
	}
	$utfusername=doUtfAndGbk($fname,0);
	$num=$empire->gettotal("select count(*) as total from ".$user_tablename." where ".$user_username."='$utfusername' limit 1");
	if(!$num)
	{
		printerror("NotFriendUsername","",1);
	}
	//重复提交
	if($fname!=$add['oldfname'])
	{
		$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewshy where fname='$fname' and userid='$user_r[userid]' limit 1");
		if($num)
		{
			printerror("ReAddFriend","",1);
		}
	}
	$cid=(int)$add['cid'];
	$fsay=RepPostStr($add['fsay']);
	$sql=$empire->query("update {$dbtbpre}enewshy set fname='".addslashes($fname)."',cid=$cid,fsay='".addslashes($fsay)."' where fid=$fid and userid='$user_r[userid]'");
	if($sql)
	{
		printerror("EditFriendSuccess","../member/friend?cid=$add[fcid]",1);
	}
	else
	{
		printerror("DbError","",1);
	}
}

//删除好友
function DelFriend($add){
	global $empire,$dbtbpre;
	//是否登陆
	$user_r=islogin();
	$fid=(int)$add['fid'];
	if(!$fid)
	{
		printerror("EmptyFriendId","",1);
	}
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewshy where fid=$fid and userid='$user_r[userid]'");
	if(!$num)
	{
		printerror("EmptyFriendId","",1);
	}
	$sql=$empire->query("delete from {$dbtbpre}enewshy where fid=$fid and userid='$user_r[userid]'");
	if($sql)
	{
		printerror("DelFriendSuccess","../member/friend?cid=$add[fcid]",1);
	}
	else
	{
		printerror("DbError","",1);
	}
}
?>